package com.igeekhome.controller;

import com.igeekhome.pojo.BaseDict;
import com.igeekhome.pojo.DCustomer;
import com.igeekhome.pojo.DSendingMaintenanceList;
import com.igeekhome.service.ICustomerService;
import com.igeekhome.service.IDiceService;
import com.igeekhome.service.IQuoteDtoService;
import com.igeekhome.service.ISendingMainListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Romeo
 */
@Controller
@RequestMapping("/page/quote")
public class QuoteDtoController {

    @Autowired
    private ISendingMainListService sendingMainListService;

    @Autowired
    private IDiceService diceService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IQuoteDtoService quoteDtoService;

    @Value("${SHIPMENT_TYPE}")
    private String SHIPMENT_TYPE;

    @Value("${PRODUCT_TYPE}")
    private String PRODUCT_TYPE;


    @RequestMapping("")
    public String quote(){
        return "pages/data/quote/quote";
    }
    @RequestMapping("/edit")
    public String edit(Model model){
        // 寄件区间，目的区间
        return handle(model);
    }
    @RequestMapping("/add")
    public String add(Model model){
        // 寄件区间，目的区间
        return handle(model);
    }

    /**
     * 查看报价信息(按钮)
     */
    @RequestMapping("/check/{offerId}")
    public String check(@PathVariable("offerId") Integer offerId,Model model){
        model.addAttribute("offerId",offerId);
        return "pages/data/quote/quoteRule";
    }
    @RequestMapping("/checkEdit/{serialId}")
    public String checkEdit(Model model,@PathVariable("serialId") Integer serialId){
        // 快件类型，物品类型，客户类型
        System.out.println(serialId);
        List<String[]> formulas = quoteDtoService.selectFormula(serialId);
        model.addAttribute("formulas",formulas);
        return checkHandle(model);
    }
    @RequestMapping("/checkAdd/{offerTypeId}")
    public String checkAdd(@PathVariable("offerTypeId") Integer offerTypeId,Model model){
        model.addAttribute("offerTypeId",offerTypeId);

        return checkHandle(model);
    }

    private String handle(Model model){
        // 寄件区间，目的区间
        List<DSendingMaintenanceList> send = sendingMainListService.selectBySectionType(0);
        List<DSendingMaintenanceList> destination = sendingMainListService.selectBySectionType(1);
        model.addAttribute("send",send);
        model.addAttribute("destination",destination);
        return "pages/data/quote/quoteHandle";
    }

    private String checkHandle(Model model){
        List<BaseDict> shifts = diceService.selectByTypeCode(SHIPMENT_TYPE);
        List<BaseDict> items = diceService.selectByTypeCode(PRODUCT_TYPE);
        List<DCustomer> customers = customerService.selectToList();
        model.addAttribute("shifts",shifts).addAttribute("items",items).addAttribute("customers",customers);
        return "pages/data/quote/quoteRuleHandle";
    }

}
